/*
   Copyright (c) 2019 Shanghai Xuanzi Technology Co. Ltd https://xuanzi.ltd
   XZMindMap is licensed under the Mulan PSL v1.
   You can use this software according to the terms and conditions of the Mulan PSL v1.
   You may obtain a copy of Mulan PSL v1 at:
      http://license.coscl.org.cn/MulanPSL
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
   PURPOSE.
   See the Mulan PSL v1 for more details.

*/

package xuanzi.openmind.renders;

import xuanzi.commons.graphics.Canvas;
import xuanzi.commons.graphics.Paint;
import xuanzi.openmind.nodes.Media;
import xuanzi.openmind.nodes.Node;

public class DefaultNodeRender {

	protected Paint paint = new Paint();


	public DefaultNodeRender() {
		// TODO Auto-generated constructor stub
	}
	
	
	public void drawNode(Canvas canvas,Node node) {
		
		paint.fill = false;
		paint.borderSize= 2;
		paint.borderColor = 0xFF000000;
		RenderBuffer buffer = node.renderBuffer;
		if(node.renderBuffer.visiable) {
			drawNodeBackground(canvas,node);
		}
		drawLines(canvas,node);
		if(node.renderBuffer.visiable) {
			drawShape(canvas,buffer);
			drawElements(canvas,node);
			drawText(canvas,buffer);
			drawMedias(canvas,node); 
			drawHandler(canvas,node);
			if(node.getMemo()!=null)
				drawMemo(canvas,buffer);
			drawLabel(canvas,buffer);
			if(node.isSelected()) {
				buffer.shapeBound.inset(-5f, -5f);
				canvas.drawRect(buffer.shapeBound,5, selectPaint);
			//	canvas.drawRect(buffer.shapeBound, selectPaint);
				buffer.shapeBound.inset(5f, 5f);
			}
		}
		
	}
	
	private void drawMemo(Canvas canvas, RenderBuffer node) {
		
		paint.fill  =true;
		node.memoBox.draw(canvas);
		//canvas.drawRect(node.memoBox, paint);
	}

	static Paint selectPaint = new Paint();
	static{
		selectPaint.borderColor = 0xaa44aa00;
		selectPaint.borderSize = 4;
		selectPaint.fill = false;
	}
	
	
	public void draw(Canvas canvas,Node node) {
		RenderBuffer render = node.renderBuffer;
		RenderConfig.primaryColor = render.textColor;
		RenderConfig.secondColor = render.lineColor;
		RenderConfig.fillColor = render.backgroundColor;
		
		paint.borderColor = RenderConfig.secondColor;
		paint.fill = false;
		paint.borderSize = 2; 
	//	canvas.drawRect(RenderBuffer.visiableBox, paint);
		drawNode(canvas, node);
		
		if(node.getNodeCount()>0) {
			for(int i=0;i<node.getNodeCount();i++) {
				Node item = node.getNode(i);
				draw(canvas, item);
			}
		}
	}


	protected void drawHandler(Canvas canvas, Node node) { 
		if(!node.isEmpty()) {
			paint.fill(RenderConfig.fillColor);
			canvas.drawOval(node.renderBuffer.handlerBox, paint);
			paint.stroke(RenderConfig.primaryColor,1);
			canvas.drawOval(node.renderBuffer.handlerBox, paint);
		}
	}


	protected void drawLabel(Canvas canvas, RenderBuffer node) {
		// TODO Auto-generated method stub
		if(node.labelBox!=null) { 
			paint.borderSize = 1;
			
			{
				paint.fill(node.labelBackgroundColor);
				canvas.drawRect(node.labelBox, paint);
				paint.borderColor = node.borderColor;
				paint.stroke(node.borderColor,1);
				canvas.drawRect(node.labelBox, paint);
			}{
				paint.fill(node.textColor);
				paint.textSize = (int) (node.textSize*0.8);
				canvas.drawText(node.label,node.labelBox.centerX(), node.labelBox.centerY()-1.2f,paint);
			}
		}
	}


	protected void drawMedias(Canvas canvas, Node node) {
		paint.fill = false;
		paint.borderSize = 2;
		paint.borderColor = 0x99888888;
	  
		
		if(node.getMedias()!=null) {
			for(Media md :node.getMedias()) {
				// canvas.drawRect(md, paint);
				md.draw(canvas);
			}
		}
	}


	protected void drawText(Canvas canvas, RenderBuffer node) {
		if(node.topic==null) {
			return ;
		}
		paint.fill(node.textColor);
		paint.textSize = node.textSize;
		canvas.drawText(node.topic, node.textBox.centerX(), node.textBox.centerY(), paint);
	}


	protected void drawElements(Canvas canvas, Node node) {
		// TODO Auto-generated method stub
		
	}


	protected void drawShape(Canvas canvas, RenderBuffer node) { 
		if(node.shape!=null) {
			node.shape.setNode(node.target);
			node.shape.draw(canvas );
		}
	}


	protected void drawLines(Canvas canvas, Node node) {
		if(node.getNodeCount()>0) {
			for(int i=0;i<node.getNodeCount();i++) {
				Node item = node.getNode(i);
				drawLine(canvas,node,item,i);
			}
		}
	}
 
	protected void drawLine(Canvas canvas, Node node, Node item, int index) {
		item.renderBuffer.line.draw(canvas, node, item);
	}


	protected void drawNodeBackground(Canvas canvas, Node node) {
		// TODO Auto-generated method stub
		//canvas.drawRect(node.renderBuffer.childBound, 3, paint);
	}
	

}
